package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.mapper.AppMapper;
import com.ruoyi.pojo.Carry;
import com.ruoyi.pojo.Prize;
import com.ruoyi.pojo.Uesr;
import com.ruoyi.service.AppService;
import com.ruoyi.service.CarryService;
import com.ruoyi.service.PrizeService;
import com.ruoyi.vo.SigninRecord;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AppServiceImpl implements AppService {

    @Resource
    private AppMapper appMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private PrizeService prizeService;

    @Autowired
    private CarryService carryService;

    @Override
    public List<SigninRecord> signinRecordList(Integer user_id) {
        List<SigninRecord> signinRecords = appMapper.siginRecordLisr(user_id);
        return signinRecords;
    }

    @Override
    public Integer signinRecord(Integer uid, Integer sid) {
        Integer num = appMapper.signinRecord(uid,sid);
        return num;
    }

    @Override
    public List<Prize> prizeNoZero() {
        List<Prize> prizeList = appMapper.prizeNoZero();
        return prizeList;
    }

    @Override
    public Boolean lotteryRaffle(Uesr user) {
        List<Prize> prizeList = this.prizeNoZero();
        for (int i = 0; i < prizeList.size(); i++) {
            Prize prize = prizeList.get(i);
            //奖池默认初始值
            Integer jackpot = 0;
            //判断该奖品奖池是否存在
            if (redisTemplate.opsForHash().get("prize",prize.getPid()+"")!=null){
                String s = (String) redisTemplate.opsForHash().get("prize",prize.getPid()+"");
                jackpot = Integer.parseInt(s);
            }
            int random = (int)(Math.random()*10);
            if (random > 3 || random <= 0){
                random = 1;
            }
            jackpot = jackpot + random;
            redisTemplate.opsForHash().put("prize",prize.getPid()+"",jackpot+"");
            //判断本次抽奖是否中奖
            if (jackpot % prize.getWithdraw() != 0 || jackpot % prize.getWithdraw() > 3){
                continue;
            }
            //奖品数量-1
            QueryWrapper<Prize> prizeQueryWrapper = new QueryWrapper<>();
            prizeQueryWrapper.eq("pid",prize.getPid());
            Integer pnum = prize.getPnum();
            prize.setPnum(pnum - 1);
            prizeService.update(prize,prizeQueryWrapper);
            //添加中奖名单
            Carry carry = new Carry();
            carry.setPid(prize.getPid());
            carry.setUid(user.getUid());
            carry.setCnum(1);
            carryService.save(carry);
            System.out.println("中奖"+prize.getPname()+"奖品，剩余数量"+prize.getPnum()+",剩余积分数:"+user.getUnum());
            return true;
        }
        System.out.println("未中奖,剩余积分数:"+user.getUnum());
        return false;
    }

    @Override
    public Integer tokenUserId(String token) {
        Claims abcdefghijklmnopqrstuvwxyz = Jwts.parser().setSigningKey("abcdefghijklmnopqrstuvwxyz").parseClaimsJws(token).getBody();
        Integer user_id = (Integer) abcdefghijklmnopqrstuvwxyz.get("user_id");
        return user_id;
    }
}
